package morenoapp.util;

/**
 *
 * @param <T> 
 * @author dmitry.mamonov
 */
public class EnumWrapper<T extends Enum> implements Description {
    protected static final String NOT_SPECIFIED = "not_specified"; //NOI18N
    protected Class<T> wrappedType;
    protected T wrappedValue;
    protected String textKey;


    public EnumWrapper(Class<T> _type, T _value) {
        this.wrappedType = _type;
        this.wrappedValue = _value;
        this.textKey = "enum."+wrappedType.getSimpleName()+"."+(_value==null?"null":_value.name());
    }

    public T getWrappedValue() {
        return wrappedValue;
    }

    @Override
    public String getHtmlDescription() {
       return I18N.get(textKey+".html");
    }

    @Override
    public String getLongTitle() {
        return I18N.get(textKey);
    }

    @Override
    public String getShortTitle() {
        return I18N.get(textKey+".short");
    }

    @Override
    public boolean equals(Object _other) {
        if (_other instanceof EnumWrapper ) {
            return this.wrappedValue == ((EnumWrapper)_other).wrappedValue;
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        return this.wrappedValue!=null?this.wrappedValue.hashCode():0;
    }

    @Override
    public String toString() {
        return I18N.get(textKey);
    }

    public static <E extends Enum> EnumWrapper<E>[] createWrappers(Class<E> _type, E... _values){
        EnumWrapper<E>[] result = new EnumWrapper[_values.length];
        int index = 0;
        for(E value:_values){
            result[index] = new EnumWrapper<E>(_type, value);
            index++;
        }
        return result;
    }

    public static <E extends Enum> EnumWrapper<E>[] createWrappersAll(Class<E> _clazz){
        return createWrappers(_clazz, _clazz.getEnumConstants());
    }

    public static <E extends Enum> EnumWrapper<E>[] createWrappersNullAndAll(Class<E> _clazz){
        EnumWrapper<E>[] allWrappers = createWrappersAll(_clazz);
        EnumWrapper<E>[] result = new EnumWrapper[1+allWrappers.length];
        result[0] = new EnumWrapper<E>(_clazz, null);
        int index = 1;
        for(EnumWrapper<E> wrapper:allWrappers){
            result[index] = wrapper;
            index++;
        }
        return result;
    }
}
